package com.ubtechinc.cruzr.libai.honglauncher;


import com.orhanobut.logger.Logger;
import com.ubtechinc.cruzr.userverify.api.CruzrSecurityApi;
import com.ubtechinc.cruzr.userverify.api.ISecurityCallBack;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by ruiqiang on 2018/5/10.
 */
public class AdministratorObserve {
    private AtomicBoolean isAdminiInit = new AtomicBoolean(false);

    public void initSecurityApi(final AdminiListener listener) {
        CruzrSecurityApi.get().init(App.getContext(), new ISecurityCallBack() {
            @Override
            public void securityModeChange(int i) {
                if (listener != null) {
                    if (i == CruzrSecurityApi.MODE_CHANGE_CODE_VISITOR) {
                        listener.onExitAdmini();
                    } else {
                        listener.onEnterAdmini();
                    }
                }
            }

            @Override
            public void onBindSuccess() {
                if (!isAdminiInit.get()) {
//                    AppManger.get().loadApps(true, isAdmini());
                }
                isAdminiInit.compareAndSet(false, true);
                Logger.i("admini  onBindSuccess");
            }

            @Override
            public void onBindfail() {
                isAdminiInit.compareAndSet(true, false);
            }

            @Override
            public void onDisconnect() {
                if (listener != null) {
                    listener.onExitAdmini();
                }
                isAdminiInit.compareAndSet(true, false);
            }
        });
    }

    public boolean isAdminiInit() {
        return isAdminiInit.get();
    }

    public boolean isAdmini() {
        return CruzrSecurityApi.get().getAdminiMode() != CruzrSecurityApi.MODE_CHANGE_CODE_VISITOR;
    }
}
